#include "Fan.h"

#include "Time.h"
#include "Physics.h"
#include "IceLayer.h"
#include "IceBlock.h"
#include "Water.h"
#include "MetalBox.h"
#include "WoodBox.h"
#include "RadiationBarrel.h"
#include "Camera.h"

// Constructor
Fan::Fan(float range) : MovingPlatform()
{
	this->objectID = "Fan";
	this->range = range;
	this->pushSpeed = 500.0f;
	this->SetCanBeGrappled(false);
}

// Desctructor
Fan::~Fan(void)
{
}

void Fan::Update(float deltaTime)
{
	if(this->GetWaypointCount() > 0)
		MovingPlatform::Update(deltaTime);

	// Get the center of the fan.
	tVector2D center = Physics::GetCenterPoint(hitBox);

	LinecastInfo hitInfo;
	if(Physics::LineCast(hitInfo, range, center, direction, this) == true)
	{
		GameObject* hitObject = hitInfo.GetObjectHit();

		auto slidingObjects = IceLayer::GetSlidingObjects();

		bool match = false;
		for(unsigned int i = 0; match == false && i < slidingObjects.size(); ++i)
			match = (hitObject == slidingObjects[i].object);

		if(match == true)
		{
			if(hitObject->GetObjectID() == "Player")
			{
				hitObject->SetPosX(hitObject->GetPosX() + direction.fX * pushSpeed * deltaTime);
			}
			else if(hitObject->GetObjectID() == "Metal Box")
			{
				MetalBox* box = dynamic_cast<MetalBox*>(hitObject);
				hitObject->SetPosX(hitObject->GetPosX() + direction.fX * pushSpeed * deltaTime);

				if(box != nullptr)
					box->ApplyHeat(this->CurTemp());
			}
			else if(hitObject->GetObjectID() == "Wood Box")
			{
				WoodBox* box = dynamic_cast<WoodBox*>(hitObject);
				hitObject->SetPosX(hitObject->GetPosX() + direction.fX * pushSpeed * deltaTime);

				if(box != nullptr)
					box->ApplyHeat(this->CurTemp());
			}
			else if(hitObject->GetObjectID() == "Radiation Barrel")
			{
				RadiationBarrel* barrel = dynamic_cast<RadiationBarrel*>(hitObject);
				hitObject->SetPosX(hitObject->GetPosX() + direction.fX * pushSpeed * deltaTime);
			}
		}

		if(hitObject->GetObjectID() == "Ice Layer")
		{
			IceLayer* ice = dynamic_cast<IceLayer*>(hitObject);

			if(ice != nullptr)
				ice->ApplyHeat(this->CurTemp());
		}
		else if(hitObject->GetObjectID() == "Ice Block")
		{
			IceBlock* ice = dynamic_cast<IceBlock*>(hitObject);

			if(ice != nullptr)
				ice->ApplyHeat(this->CurTemp());
		}
		else if(hitObject->GetObjectID() == "Water")
		{
			Water* water = dynamic_cast<Water*>(hitObject);

			if(water != nullptr)
				water->ApplyHeat(this->CurTemp());
		}
	}
}
void Fan::Render(void) const
{
	int a = 255, r = 255, g = 255, b = 255;

	if(this->CurTemp() < 0.0f)
	{
		r = g = (int)((this->CurTemp() / this->MaxTemp()) * 255.0f);
	}
	else if(this->CurTemp() > 0.0f)
	{
		g = b = (int)((this->CurTemp() / this->MinTemp()) * 255.0f);
	}

	CSGD_TextureManager::GetInstance()->Draw(imageID,
		(int)(GetPosX() - Camera::GetInstance()->OffsetX()),
		(int)(GetPosY() - Camera::GetInstance()->OffsetY()),
		1.0f, 1.0f, nullptr, 0.0f, 0.0f, 0.0f, D3DCOLOR_ARGB(a, r, g, b)
		);

#if 1
	tVector2D center = Physics::GetCenterPoint(hitBox);
	/*CSGD_Direct3D::GetInstance()->DrawLine(
		(int)(center.fX - Camera::GetInstance()->OffsetX()),
		(int)(center.fY - Camera::GetInstance()->OffsetY()),
		(int)(center.fX + (direction.fX * range) - Camera::GetInstance()->OffsetX()),
		(int)(center.fY + (direction.fY * range) - Camera::GetInstance()->OffsetY()),
		D3DCOLOR_ARGB(255, 0, 0, 255), 2
		);*/
#endif
}
void Fan::HandleCollision(GameObject& other, CollisionInfo& info)
{
	
}
void Fan::ResetToCheckPoint(void)
{

}

void Fan::SetRange(float range)
{
	this->range = range;
}

void Fan::SetPushSpeed(float speed)
{
	this->pushSpeed = speed;
}

// SetDirection
//	- Set the direction the laser is facing.
//	- The x and y values should be the values of a normalized vector.
void Fan::SetDirection(float x, float y)
{
	this->direction.fX = x;
	this->direction.fY = y;
}

// SetDirection
//	- Set the direction the laser is facing.
//	- The vector should be normalized.
void Fan::SetDirection(tVector2D direction)
{
	this->direction = direction;
}